package com.finstone.data.spider.eshop.service.impl;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Spider;

import com.finstone.data.spider.eshop.pipeline.EShopGoodsDetailPipline;
import com.finstone.data.spider.eshop.processor.EShopGoodsDetailProcessor;
import com.finstone.data.spider.eshop.service.AbstractSpiderService;

@Component
public class EShopGoodsDetailService extends AbstractSpiderService<String> {

	@Autowired
	private EShopGoodsDetailProcessor eShopGoodsDetailProcessor;

	@Autowired
	private EShopGoodsDetailPipline eShopGoodsDetailPipline;

	@Override
	@PostConstruct
	public void init() {
		spider = Spider.create(eShopGoodsDetailProcessor).addPipeline(eShopGoodsDetailPipline).thread(5);
	}

	@Override
	protected void doPutTask(String object) {
		String[] params = object.split(";");

		String url = "http://www.lenovo.com.cn/product/"+params[0]+".html";
		Request request = new Request(url);
		request.putExtra("goods_id", params[0]);
		request.putExtra("price", params[1]);
		request.putExtra("name", params[2]);
		spider.addRequest(request);
	}
}
